package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.glory.cqaospda.R
import com.glory.cqaospda.adapter.ProductionInfoAdapter
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.bean.QueryProductionInfoBean
import com.glory.cqaospda.utils.EditTextEnterUtils
import com.glory.cqaospda.viewmodel.QueryProductionInfoModel
import com.glory.mvvmnet.base.BaseActivity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText
import kotlinx.android.synthetic.main.activity_queryeqpinfo.*
import kotlinx.android.synthetic.main.activity_title.*
import kotlinx.android.synthetic.main.production_layout.*

class QueryProductionInfoActivity : BaseActivity<QueryProductionInfoModel>() {

    var productionInfoAdapter: ProductionInfoAdapter? = null
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when (msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@QueryProductionInfoActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    override fun initData() {

        queryEqpData(etEqpId)

        viewModel.defUI.callObserve.observe(this@QueryProductionInfoActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        val queryProductionInfoBean = it.item as QueryProductionInfoBean
                        productionInfoAdapter!!.setList(queryProductionInfoBean.mProductList)
                    }
                    2 -> {
                        productionInfoAdapter!!.setList(null)
                    }
                }
            } else {
                Constant.okgoError = false
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        tvTitle.text = "生产信息查询"
        tvUserName.text = Constant.UserName
        tvBaseSubmit.visibility = View.GONE
        tvLotQty.visibility = View.VISIBLE
        tvLotID.visibility = View.VISIBLE
        initRecyclerView(rvProductionList, this)
        productionInfoAdapter = ProductionInfoAdapter(null)
        rvProductionList.adapter = productionInfoAdapter

        val instance = MyApp.instance
        instance!!.handler = handler
    }

    override fun layoutId(): Int {
        return R.layout.activity_productioninfo
    }

    protected fun initRecyclerView(recyclerView: RecyclerView, context: Context?) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.itemAnimator = DefaultItemAnimator()
        recyclerView.addItemDecoration(DividerItemDecoration(context, 1))
    }

    private fun queryEqpData(
        editText: FreeUI_ClearEditText?,
    ) {
        EditTextEnterUtils.initEnterToUpperCase(editText)
        FreeApi_EditTextUtils.setEditTextListener(editText, this) { s: String? ->
            productionInfoAdapter!!.setList(null)
            viewModel.defaultEqpInfo(s!!)
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == 520) {
            try {
                val view = window.decorView.findFocus() as EditText
                when (view) {
                    etEqpId -> {
                        etEqpId.setText("")
                    }
                }
            } catch (e: Exception) {
                println(e.message)
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}